//
// Created by 66477 on 2022/9/24.
//

#ifndef DATA_STRUCTURE_LIST3_2_H
#define DATA_STRUCTURE_LIST3_2_H

#include <cstring>
#include "Node3_2.h"
#include "OList3_2.h"

class List3_2 {
private:
    Node3_2 *head = nullptr;//单链表表头
    OList3_2 *num = new OList3_2();//数字字符循环链表
    OList3_2 *letter = new OList3_2();//字母字符循环链表
    OList3_2 *symbols = new OList3_2();//符号字符循环链表

public:
    /**
     * 构造函数传入字符串解析成链表存head
     * @param s 需要插入和解析的字符串
     */
    List3_2(string s) {
        for (int i = 0; i < s.length(); ++i) {
            add(s[i]);
        }
    }

    /**
     * 尾插添加元素
     * @param c 插入的值
     * @return
     */
    List3_2 *add(char c) {
        Node3_2 *p = new Node3_2(c), *q = head;
        if (head == nullptr) {
            head = p;
        } else {
            while (q->getNext() != nullptr) {
                q = q->getNext();
            }
            q->setNext(p);
        }
        return this;
    }

    /**
     * 遍历单链表并分类，插入对应循环链表
     * @return
     */
    List3_2 *classification() {
        if (head != nullptr) {
            Node3_2 *p = head, *q;
            while (p != nullptr) {
                q = p;
                p = p->getNext();
                classificationSingle(q);
            }
            letter->listGo();
            num->listGo();
            symbols->listGo();
        }
        return this;
    }

    /**
     * 把传入的节点分类处理插入到对应循环链表
     * @param n 需要处理的节点
     */
    void classificationSingle(Node3_2 *n) {
        char c = n->getDate();
        if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
            letter->insert(n);
        } else if (c >= '0' && c <= '9') {
            num->insert(n);
        } else {
            symbols->insert(n);
        }
    }

    /**
    * 打印链表
    */
    List3_2 *print() {
        cout << "Q2 List:";
        if (head != nullptr) {
            Node3_2 *q = head;
            while (q != nullptr) {
                cout << q->getDate() << " ";
                q = q->getNext();
            }
        }
        cout << endl;
        return this;
    }

    /**
     * 打印3个循环链表
     * @return
     */
    List3_2 *print3Classification() {
        cout << "number:";
        num->print();
        cout << "letter:";
        letter->print();
        cout << "symbols:";
        symbols->print();
        return this;
    }
};

#endif //DATA_STRUCTURE_LIST3_2_H
